package cn.pengyinrui.department.system.login.controller;

import cn.pengyinrui.department.system.login.domain.LoginRO;
import cn.pengyinrui.department.system.user.domain.User;
import cn.pengyinrui.department.system.user.service.IUserService;
import cn.pengyinrui.department.utils.AjaxResult;
import cn.pengyinrui.department.utils.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/logininfo")
@Api(value = "账号登录的API",description = "账号登录的功能")
public class Login {
    @Autowired
    private IUserService userService;

    @PostMapping("/account")
    @ApiOperation(value = "账号登录")
    public AjaxResult account(@RequestBody LoginRO ro){
        User user = userService.findOneByPhone(ro.getPhone());
        try {
            if(user == null){
                return AjaxResult.error().setTip("用户名或密码错误");
            }
            if(!user.equals(ro.getPassword())){
                return AjaxResult.error().setTip("用户名或密码错误");
            }
            return AjaxResult.success();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.error().setTip(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setTip("网络异常，请稍后再试");
        }
    }
}
